package com.google.shoopinggoods;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.shoppinggoods.R;
import com.google.shoopinggoods.base.BaseActivity;
import com.google.shoopinggoods.fragment.CartFragment;
import com.google.shoopinggoods.fragment.HomeFragment;
import com.google.shoopinggoods.fragment.MeFragment;

import java.util.ArrayList;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.activity_homepage)
    RadioButton activityHomepage;
    @BindView(R.id.activity_cart)
    RadioButton activityCart;
    @BindView(R.id.activity_me)
    RadioButton activityMe;
    @BindView(R.id.activity_group)
    RadioGroup activityGroup;
    private ArrayList<Fragment> fragments;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void setListener() {
        activityGroup.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData() {
        prepareFragments();
        showFragment(0);
    }

    private void showFragment(int position) {
        switch (position){
            case 0:
                showTitle(false,"首页","");
                break;
            case 1:
                showTitle(false,"购物车","");
                break;
            case 2:
                showTitle(false,"我","");
                break;
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            if (i==position){
                //展示
                if (fragment.isAdded()){
                    transaction.show(fragment);
                }else{
                    transaction.add(R.id.activity_container,fragment);
                }
            }else{
                //隐藏
                if (fragment.isAdded()){
                   transaction.hide(fragment);
                }
            }
        }
        transaction.commitAllowingStateLoss();
    }

    private void prepareFragments() {
        fragments = new ArrayList<Fragment>();
        fragments.add(new HomeFragment());
        fragments.add(new CartFragment());
        fragments.add(new MeFragment());
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId){
            case R.id.activity_homepage:
                showFragment(0);
                break;
            case R.id.activity_cart:
                //需先判断是否登陆
                if(false){
                    showFragment(1);
                }else{
                    activityHomepage.setChecked(true);
                    startActivity(new Intent(MainActivity.this,LoginActivity.class));
                }
                break;
            case R.id.activity_me:
                showFragment(2);
                break;
        }
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        moveTaskToBack(true);
    }
}
